<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Adv;
use app\common\model\Banner;
use app\common\model\Cate;
use app\common\model\Shop;
use app\common\model\Supply as SupplyModel;

class Supply
{
    //程宇
    //获取供应大厅
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取banner
            'cate' => $this->cate(),//获取分类
        ]);
    }

    /**
     * 获取banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 2])
            ->field('id,title,image')
            ->limit(0, 1)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }

    /**
     * 获取大分类
     */
    public function cate()
    {
        $model = New Cate();
        $cate = $model->where(['status' => 1, 'type' => 4])
            ->field('id,name')
            ->limit(0, 6)
            ->select();
        return $cate;
    }

    /**
     * 获取小分类
     */
    public function cates()
    {
        $pid = input('id/d');
        if (empty($pid)) {
            return errMsg('200', '暂无数据');
        }
        $page = input('page/d', 1);
        $pagesaize = 16;
        $model = New Cate();
        $cate1 = $model->where(['status' => 1, 'type' => 4, 'pid' => 0])->column('id');
        $cates = $model->where(['status' => 1, 'type' => 4, 'pid' => $pid])
            ->field('id,name,pid')
            ->page($page, $pagesaize)
            ->select()->toArray();

        $pid1 = array_column($cates, 'id');
        $cates2 = $model->where(['status' => 1, 'type' => 4, 'pid' => $pid1])
            ->field('id,name,pid')
            ->page($page, $pagesaize)
            ->select()->toArray();
        $a = array_merge($cates, $cates2);
        // 检验必须为数组
        if (!is_array($a)) {
            return false;
        }
        $tree = [];
        foreach ($a as $k => $v) {
            foreach ($cate1 as $key => $value) {
                if ($v['pid'] == $value) {
                    $tree[$k] = $v;
                    $tree[$k]['children'] = getTree($a, $v['id']);
                }
                unset($a[$k]);
            }
        }
        return sucessMsg('200', '获取成功', ['page' => $page, 'pagesize' => $pagesaize, 'tree' => $tree]);
    }

    //获取列表
    public function list()
    {
        $cate_id = input('id/d');
        $page = input('page/d', 1);
        $pagesize = 16;
        $supply = New SupplyModel();
        if (empty($cate_id)) {
            $list = $supply->where('status', 1)
                ->field('id,title,image,price,turnover')
                ->order('id', 'desc')
                ->page($page, $pagesize)
                ->select();
            $count = count($supply->where('status', 1)->select());
            $maxpage = ceil($count / $pagesize);
            foreach ($list as $v) {
                $v['image'] = env('app.yuming') . $v['image'];
            }
            $tuijian = $supply->where('status', 1)
                ->field('id,title,image,price,turnover')
                ->order('id', 'asc')
                ->limit(0, 5)
                ->select();
            foreach ($tuijian as $v) {
                $v['image'] = env('app.yuming') . $v['image'];
            }
            return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize' => $pagesize, 'tuijian' => $tuijian]);
        } else {
            $cate = New Cate();
            $cates = $cate->where(['status' => 1, 'id' => $cate_id])
                ->field('id,pid')
                ->find();
            if ($cates['pid'] == 0) {
                $cates = $cate->where(['status' => 1, 'id' => $cate_id])
                    ->field('id,pid')
                    ->select()->toArray();
                $pid = array_column($cates, 'id');
                $aa = $cate->where('status', 1)
                    ->where('pid', 'in', $pid)
                    ->order('id', 'desc')
                    ->field('id,name')
                    ->select()->toArray();
                //dd($aa);
                $pid2 = array_column($aa, 'id');
                $bb = $cate->where('status', 1)
                    ->where('pid', 'in', $pid2)
                    ->order('id', 'desc')
                    ->field('id,name')
                    ->select()->toArray();
                $cate_id = array_column($bb, 'id');
                $list = $supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,title,image,price,turnover')
                    ->order('id', 'desc')
                    ->page($page, $pagesize)
                    ->select();
                foreach ($list as $v) {
                    $v['image'] = env('app.yuming') . $v['image'];
                }
                $count = count($supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->select());
                $maxpage = ceil($count / $pagesize);
                $tuijian = $supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,title,image,price,turnover')
                    ->order('id', 'asc')
                    ->limit(0, 5)
                    ->select();
                foreach ($tuijian as $v) {
                    $v['image'] = env('app.yuming') . $v['image'];
                }
                return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize' => $pagesize, 'tuijian' => $tuijian]);
            } else {
                $aa = $cate->where(['status' => 1, 'id' => $cate_id])
                    ->order('id', 'desc')
                    ->field('id,name')
                    ->select()->toArray();
                $cate_id = array_column($aa, 'id');
                $list = $supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,title,image,price,turnover')
                    ->order('id', 'desc')
                    ->page($page, $pagesize)
                    ->select();
                foreach ($list as $v) {
                    $v['image'] = env('app.yuming') . $v['image'];
                }
                $count = count($supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->select());
                $maxpage = ceil($count / $pagesize);
                $tuijian = $supply->where('status', 1)
                    ->where('cate_id', 'in', $cate_id)
                    ->field('id,title,image,price,turnover')
                    ->order('id', 'asc')
                    ->limit(0, 5)
                    ->select();
                foreach ($tuijian as $v) {
                    $v['image'] = env('app.yuming') . $v['image'];
                }
                return sucessMsg('200', '获取成功', ['list' => $list, 'count' => $count, 'page' => $page, 'maxpage' => $maxpage, 'pagesize' => $pagesize, 'tuijian' => $tuijian]);
            }
        }
    }


    public function content()
    {
        $id = input('id/d');
        if (empty($id)) {
            return errMsg('400', '获取失败，请传入id');
        }
        $model = New SupplyModel();
        $content1 = $model->where(['status' => 1, 'id' => $id])
            ->field('id,image,title,price,purchasing,address,logistics,guige,see,comment,created_at')
            ->find();
        if (empty($content1)) {
            return errMsg('400', '获取失败，暂无数据');
        }
        $content1['image'] = env('app.yuming') . $content1['image'];
        $rexiao = $model->where('status', 1)
            ->order('id', 'desc')
            ->field('id,price,title,image')
            ->limit(0, 2)
            ->select();
        foreach ($rexiao as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        $adv = New Adv();
        $advs = $adv->where(['status' => 1, 'type' => 2])
            ->field('id,image')
            ->find();
        $advs['image'] = env('app.yuming') . $advs['image'];
        $shop = New Shop();
        $dianpu = $shop->where(['status' => 1, 'id' => 1])
            ->field('id,name,logo,turnover,scored,visitor,address,main')
            ->find();
        $dianpu['logo'] = env('app.yuming') . $dianpu['logo'];
        $content2 = $model->where(['status' => 1, 'id' => $id])
            ->field('id,image,varieties,weight,type,environment,purpose,content')
            ->find();
        $content2['image'] = env('app.yuming') . $content2['image'];
        return sucessMsg('200', '获取成功', ['content1' => $content1, 'rexiao' => $rexiao, 'adv' => $advs, 'dianpu' => $dianpu, 'content2' => $content2]);
    }

}
